package com.chhuang.ewcrm.thread;

import com.chhuang.ewcrm.cons.Constants;
import com.chhuang.ewcrm.ctrl.WeighController;
import javafx.scene.control.Label;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static javafx.application.Platform.runLater;

/**
 * @author chhuang
 * @create 2020-11-26-10:27
 */
public class AutoWeighThread implements Runnable{
    private final Logger logger = LoggerFactory.getLogger(AutoWeighThread.class);

    private Label weighLEDLabel;
    private WeighController weighController;

    public AutoWeighThread(Label weighLEDLabel, WeighController weighController){
        this.weighLEDLabel = weighLEDLabel;
        this.weighController = weighController;
    }

    @SneakyThrows
    @Override
    public void run() {
        Thread.sleep(10000);//车辆上磅时间
        String id = weighLEDLabel.getId();
        while(StringUtils.isEmpty(id) || !"1".equals(id)){
            id = weighLEDLabel.getId();
            logger.debug("自动过磅显示是否稳定："+id);
            Thread.sleep(2000);
        }
        String weighStr = weighLEDLabel.getText();
        if(StringUtils.isNotEmpty(weighStr) && !Constants.NO_SIGN_STR.equals(weighStr)){
            double weigh = Double.parseDouble(weighStr);
            if(weigh != 0){
                runLater(()->weighController.getWeigh());
            }
        }
    }

}
